import http from '@ohos.net.http'
import { ResponseModel } from '../model/ResponseModel';
import { ConfigUtil as Config } from './ConfigUtil';
import preferencesUtil from './PreferencesUtil';

export async function  HttpGet<T> (apiURI: string, extraData?: object): Promise<ResponseModel> {
  let httpRequest: http.HttpRequest = http.createHttp()
  let token = await preferencesUtil.getValue('token', '');
  let requestOptions: http.HttpRequestOptions={
    method: http.RequestMethod.GET,
    header: {
      // 'Content-Type': 'application/json',
      'Content-Type': 'application/x-www-form-urlencoded',
      'Authorization': token
    },
    extraData: extraData,
    connectTimeout: Config.HTTP_CONNECT_TIMEOUT,
    readTimeout: Config.HTTP_READ_TIMEOUT,
  }
  let promiseData: Promise<http.HttpResponse> = httpRequest.request(apiURI, requestOptions)
  let serverData: ResponseModel = new ResponseModel()
  return promiseData.then((response) => {
    if (response.responseCode === Config.HTTP_CODE_200) {
      let data = `${response.result}` //把json对象转换成json字符串
      serverData.success = true
      serverData.message = Config.SERVER_NET_SUCCESS
      let result: object = JSON.parse(data) //把json字符串转换成json对象
      serverData.result = result;
      return serverData;
    } else {
      serverData.message = Config.SERVER_NET_ERROR
      return serverData;
    }
  }).catch((e:Error) => {
    serverData.message = `${Config.SERVER_NET_ERROR}, ${e}`;
    return serverData;
  })
}

export async function HttpPost<T>(apiURI: string, extraData?: object): Promise<ResponseModel> {
  let httpRequest: http.HttpRequest = http.createHttp()
  let token = await preferencesUtil.getValue('token', '');
  let requestOptions: http.HttpRequestOptions={
    method: http.RequestMethod.POST,
    header: {
      // 'Content-Type': 'application/json',
      'Content-Type': 'application/x-www-form-urlencoded',
      'Authorization': token
    },
    extraData: extraData,
    connectTimeout: Config.HTTP_CONNECT_TIMEOUT,
    readTimeout: Config.HTTP_READ_TIMEOUT
  }
  let promiseData: Promise<http.HttpResponse> = httpRequest.request(apiURI, requestOptions)
  let serverData: ResponseModel = new ResponseModel()
  return promiseData.then((response) => {
    if (response.responseCode === Config.HTTP_CODE_200) {
      let data = `${response.result}` //把json对象转换成json字符串
      serverData.success = true
      serverData.message = Config.SERVER_NET_SUCCESS
      let result: object = JSON.parse(data) //把json字符串转换成json对象
      serverData.result = result
      return serverData
    } else {
      serverData.message = Config.SERVER_NET_ERROR
      return serverData
    }
  }).catch((e:Error) => {
    serverData.message = `${Config.SERVER_NET_ERROR}, ${e}`;
    return serverData;
  })
}

export async function HttpPut<T>(apiURI: string, extraData?: object): Promise<ResponseModel> {
  let httpRequest: http.HttpRequest = http.createHttp()
  let token = await preferencesUtil.getValue('token', '');
  let requestOptions: http.HttpRequestOptions={
    method: http.RequestMethod.PUT,
    header: {
      // 'Content-Type': 'application/json',
      'Content-Type': 'application/x-www-form-urlencoded',
      'Authorization': token
    },
    extraData: extraData,
    connectTimeout: Config.HTTP_CONNECT_TIMEOUT,
    readTimeout: Config.HTTP_READ_TIMEOUT
  }
  let promiseData: Promise<http.HttpResponse> = httpRequest.request(apiURI, requestOptions)
  let serverData: ResponseModel = new ResponseModel()
  return promiseData.then((response) => {
    if (response.responseCode === Config.HTTP_CODE_200) {
      let data = `${response.result}` //把json对象转换成json字符串
      serverData.success = true
      serverData.message = Config.SERVER_NET_SUCCESS
      let result: object = JSON.parse(data) //把json字符串转换成json对象
      serverData.result = result
      return serverData;
    } else {
      serverData.message = Config.SERVER_NET_ERROR
      return serverData;
    }
  }).catch((e:Error) => {
    serverData.message = `${Config.SERVER_NET_ERROR}, ${e}`;
    return serverData;
  })
}

export async function HttpDelete<T>(apiURI: string, extraData?: object): Promise<ResponseModel> {
  let httpRequest: http.HttpRequest = http.createHttp()
  let token = await preferencesUtil.getValue('token', '');
  let requestOptions: http.HttpRequestOptions={
    method: http.RequestMethod.DELETE,
    header: {
      // 'Content-Type': 'application/json',
      'Content-Type': 'application/x-www-form-urlencoded',
      'Authorization': token
    },
    extraData: extraData,
    connectTimeout: Config.HTTP_CONNECT_TIMEOUT,
    readTimeout: Config.HTTP_READ_TIMEOUT
  }
  let promiseData: Promise<http.HttpResponse> = httpRequest.request(apiURI, requestOptions)
  let serverData: ResponseModel = new ResponseModel()
  return promiseData.then((response) => {
    if (response.responseCode === Config.HTTP_CODE_200) {
      let data = `${response.result}` //把json对象转换成json字符串
      serverData.success = true
      serverData.message = Config.SERVER_NET_SUCCESS
      let result: object = JSON.parse(data) //把json字符串转换成json对象
      serverData.result = result;
      return serverData
    } else {
      serverData.message = Config.SERVER_NET_ERROR
      return serverData
    }
  }).catch((e:Error) => {
    serverData.message = `${Config.SERVER_NET_ERROR}, ${e}`;
    return serverData;
  })
}